// 格式化日期时间
export const formatDateTime = (date) => {
  const options = {
    weekday: 'long',
    year: 'numeric',
    month: 'long',
    day: 'numeric',
    hour: '2-digit',
    minute: '2-digit',
    second: '2-digit'
  }
  return date.toLocaleDateString('zh-CN', options)
}

// 计算时间差
export const calculateTimeLeft = (targetDate, currentDate) => {
  const diff = targetDate - currentDate
  
  if (diff <= 0) {
    return {
      days: 0,
      hours: 0,
      minutes: 0,
      seconds: 0
    }
  }

  const days = Math.floor(diff / (1000 * 60 * 60 * 24))
  const hours = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60))
  const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
  const seconds = Math.floor((diff % (1000 * 60)) / 1000)

  return {
    days: days.toString().padStart(2, '0'),
    hours: hours.toString().padStart(2, '0'),
    minutes: minutes.toString().padStart(2, '0'),
    seconds: seconds.toString().padStart(2, '0')
  }
}

// 格式化时间
export const formatTime = (date) => {
  const hour = date.getHours().toString().padStart(2, '0')
  const minute = date.getMinutes().toString().padStart(2, '0')
  return `${hour}:${minute}`
}

// 计算工作日数量
export const getWorkDaysInMonth = (year, month, workdays) => {
  const startDate = new Date(year, month, 1)
  const endDate = new Date(year, month + 1, 0)
  let workDaysCount = 0

  for (let d = startDate; d <= endDate; d.setDate(d.getDate() + 1)) {
    const weekday = d.getDay() || 7
    if (workdays.includes(weekday)) {
      workDaysCount++
    }
  }

  return workDaysCount
}

// 计算年度工作日数量
export const getWorkDaysInYear = (year, workdays) => {
  const startDate = new Date(year, 0, 1)
  const endDate = new Date(year, 11, 31)
  let workDaysCount = 0

  for (let d = startDate; d <= endDate; d.setDate(d.getDate() + 1)) {
    const weekday = d.getDay() || 7
    if (workdays.includes(weekday)) {
      workDaysCount++
    }
  }

  return workDaysCount
}

// 计算两个日期之间的工作日数量
export const getWorkDaysBetweenDates = (startDate, endDate, workdays) => {
  let workDaysCount = 0

  for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {
    const weekday = d.getDay() || 7
    if (workdays.includes(weekday)) {
      workDaysCount++
    }
  }

  return workDaysCount
} 